<?php
/**
 * Project: firework-cms             >>  http://firework-cms.googlecode.com
 * Authors: YouniS Ben Salah  (-.-)  >>  http://younishd.kegtux.org
 *          Fabien Wang       (^.^)  >>  http://fabienwang.fr
 * License: See README and LICENSE in the top level directory
 */
 
/**
 * Module: Controller [C]
 * Intercepts the user input and controls the flow of the webapp.
 */

require_once 'core/cl_config.php';
require_once 'core/cl_model.php';
require_once 'core/cl_view.php';

/**
 * Class: Controller
 * represents the [C] module.
 */
class Controller
{
    // ctor
    public function __construct( &$input )
    {
        // init static config class.
        $this->initConf();
        
        $this->input = $input;
        
        // Debug the GET and POST arrays
        APP_CONFIG::DISPLAY_DEBUG( 'debug', $this->input );
        
        $this->view = new View();
        
        // set action
        $this->action = isset( $this->input['do'] ) && !empty( $this->input['do'] ) ? $this->input['do'] : APP_CONFIG::$DEFAULT_ACTION;
	}
    
    /**
     * Method: display()
     * Returns the final html5 code ( from View module ).
     */
    public function display()
    {
        // add home link
        $this->view->addData( 'home', '?do=' . APP_CONFIG::$DEFAULT_ACTION );
        
        // check if action is in pages list
        // if action is a page then set view unit
        // else set view unit to 404.
        $pages_list_model = new PagesListModel();
        $pages_list_array = $pages_list_model->retrieve('all');
        if ( in_array( $this->action, $pages_list_array ) ) {
            $this->view->setUnit( $this->action );
            $this->view->addData( 'user_input', $this->input );
        }
        else {
            // 404
            $this->view->setUnit( '404' );
        }
        // generate and return code
        return $this->view->genCode();
    }
    
    /**
     * Method: initConf()
     * sets the config at runtime from model.
     */
    protected function initConf()
    {
        // set the app conf in static class at runtime.
        $appconf_model = new AppConfModel();
        APP_CONFIG::$DEBUG_LVL = intval($appconf_model->retrieve('debug_lvl'));
        APP_CONFIG::$DEFAULT_ACTION = $appconf_model->retrieve('default_action');
        APP_CONFIG::$NB_RECENT_POSTS = intval($appconf_model->retrieve('nb_recent_posts'));
        APP_CONFIG::$ERRMSG_404 = $appconf_model->retrieve('errmsg_404');
        APP_CONFIG::$WRITE_LOG = intval($appconf_model->retrieve('write_log'));
        APP_CONFIG::$LOG_FILE = $appconf_model->retrieve('log_file');
        
        // set the db conf in static class at runtime.
        $dbconf_model = new DBConfModel();
        DB_CONFIG::$DB_SETTINGS['ENGINE'] = $dbconf_model->retrieve('engine');
        DB_CONFIG::$DB_SETTINGS['HOST'] = $dbconf_model->retrieve('host');
        DB_CONFIG::$DB_SETTINGS['NAME'] = $dbconf_model->retrieve('name');
        DB_CONFIG::$DB_SETTINGS['USER'] = $dbconf_model->retrieve('user');
        DB_CONFIG::$DB_SETTINGS['PWD'] = $dbconf_model->retrieve('pwd');
        
        // set the templ conf in static class at runtime.
        $templconf_model = new TemplateModel();
        TEMPLATE_CONFIG::$DEFAULT_TEMPLATE = $templconf_model->retrieve('template');
    }
    
    // final view
    protected $view;
    
    // user input
    protected $input;
    
    // current action
    protected $action;
}
